package com.fecean.aabbc.netty.client.prod7;

import io.netty.util.HashedWheelTimer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 ** Netty客户端启动类
 *
 * @author: chenfenghai
 * @version: 1.0
 * @date: 2020-02-24 10:14
 * @blame Team
 */
@Slf4j
@Component
public class NettyClientStarter {

    /**
     ** 定时器
     */
    private final HashedWheelTimer timer = new HashedWheelTimer();

    /**
     ** 影院属性配置
     */
    @Resource
    CinemaConfig cinemaConfig;

    /**
     ** 定时任务
     */
    @Resource
    NettyClientTimerTask nettyClientTimerTask;

    /**
     ** 连接
     */
    public void connect() {
        nettyClientTimerTask.run();
    }

    /**
     ** 重新连接
     */
    public void reconnect(){
        // 延迟时间，单位：秒
        long delay = 5;
        log.info("客户端[{}-{}]连接断开，{}秒后将重新连接...",
                cinemaConfig.getTenantId(), cinemaConfig.getCinemaCode(), delay);
        timer.newTimeout(nettyClientTimerTask, delay, TimeUnit.SECONDS);
    }
}
